package com.da.web.assign_bed.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.da.common.Result;
import com.da.exception.MyException;
import com.da.web.assign_bed.entity.*;
import com.da.web.assign_bed.service.AssignBedService;
import com.da.web.school_student.entity.SchoolStudent;
import com.da.web.school_student.service.SchoolStudentService;
import com.da.web.select_bed.service.StuBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author da
 * @time 2023/12/12 上午 11:06
 */
@RestController
@RequestMapping("/api/assign")
public class AssignBedController {
    @Autowired
    private AssignBedService assignBedService;
    @Autowired
    private SchoolStudentService schoolStudentService;
    @Autowired
    private StuBedService stuBedService;

    //保存分配宿舍信息
    @PostMapping("/assignSaveBed")
    public Result assignSaveBed(@RequestBody List<AssignBed> list) {
        boolean b = assignBedService.saveBatch(list);
        if (b) {
            return Result.success("分配宿舍成功");
        } else {
            return Result.error("分配宿舍失败");
        }
    }

    @GetMapping("/getAssignBedList")
    public Result getAssignBedList(Long classId) {
        //获取宿舍列表
        List<RoomVo> roomVoList = assignBedService.getRoomVoList(classId);
        for (RoomVo roomVo : roomVoList) {
            if (roomVo == null) throw new MyException(500, "没有查询到班级对应的宿舍");
            //获取宿舍对应的床位
            Long roomId = roomVo.getRoomId();
            List<BedVo> bedVoList = assignBedService.getBedVoList(roomId);
            //给对应的宿舍设置床位
            roomVo.setChildren(bedVoList);
        }
        return Result.success("查询成功", roomVoList);
    }

    @DeleteMapping("/{bedId}/{classId}")
    public Result delete(@PathVariable("bedId") Long bedId, @PathVariable("classId") Long classId) {
        QueryWrapper<AssignBed> query = new QueryWrapper<>();
        query.lambda().eq(AssignBed::getBedId, bedId)
                .eq(AssignBed::getClassId, classId);
        boolean remove = assignBedService.remove(query);
        if (remove) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //通过班级id获取班级对应的宿舍
    @GetMapping("/getRoomByClassId")
    public Result getRoomByClassId(Long userId, String userType) {
        if (!userType.equals("0")) {
            return Result.error("学生才能参与选宿舍");
        }
        //获取学生信息
        SchoolStudent student = schoolStudentService.getById(userId);
        List<SelectRoom> roomList = null;
        if (student != null) {
            //获取班级对应的宿舍
            List<SelectRoom> tempList = assignBedService.getRoomByClassId(student.getClassId());
            //过滤出对应的学生宿舍
            roomList = tempList.stream().filter(i -> i.getSex().equals(student.getSex())).collect(Collectors.toList());
            for (SelectRoom room : roomList) {
                //获取宿舍对应的床位
                List<SelectBed> bedList = assignBedService.getBedByClassId(student.getClassId(), room.getRoomId());
                //给每个宿舍设置床位
                room.setChildren(bedList);
            }
        }
        return Result.success("查询成功", roomList);
    }

    @PostMapping("/cleanBed")
    public Result cleanBed(@RequestBody CleanParm parm) {
        stuBedService.cleanBed(parm.getClassId());
        return Result.success("清空成功");
    }

}
